package com.gregbugaj.gps.cmd;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;

import com.gregbugaj.gps.CommandType;
import com.gregbugaj.gps.Direction;
import com.gregbugaj.gps.Latitude;
import com.gregbugaj.gps.Longitude;

public class GPGLLCmd extends Command {

	public GPGLLCmd(CommandType type, String commandString) {
		super(type, commandString);
	}

	/**
	 * Ref:http://gpsinformation.org/dale/nmea.htm#nmea
	 GLL - Geographic Latitude and Longitude is a holdover from Loran data and some 
	 old units may not send the time and data active information if they are emulating Loran data. 
	 If a gps is emulating Loran data they may use the LC Loran prefix instead of GP.

	  $GPGLL,4916.45,N,12311.12,W,225444,A,*1D
	
	Where:
	     GLL          Geographic position, Latitude and Longitude
	     4916.46,N    Latitude 49 deg. 16.45 min. North
	     12311.12,W   Longitude 123 deg. 11.12 min. West
	     225444       Fix taken at 22:54:44 UTC
	     A            Data Active or V (void)
	     *iD          checksum data

	 */
	@Override
	public HashMap<String, Object>  parse() {
		String[] parts=commandstring.split(",");
		
		Latitude lat=new Latitude(parts[0], Direction.fromValue( parts[1]));
		Longitude lon=new Longitude(parts[2], Direction.fromValue( parts[3]));
		
		String datePart=parts[4];
		Calendar cal=GregorianCalendar.getInstance();
		cal.set(Calendar.HOUR, Integer.parseInt(datePart.substring(0, 2)));
		cal.set(Calendar.MINUTE, Integer.parseInt(datePart.substring(2, 4)));
		cal.set(Calendar.SECOND, Integer.parseInt(datePart.substring(4, 6)));

		
		HashMap<String, Object>  results=new HashMap<String, Object>();
		results.put("latitude", lat);
		results.put("longitude", lon);
		results.put("time", cal.getTime());
		
		return results;
	}
}
